package com.helloworld;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.helloworld.utils.PermissionUtil;
import com.helloworld.view.ReadRawsActivity;
import com.jph.takephoto.app.TakePhotoActivity;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.model.TakePhotoOptions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends TakePhotoActivity {
    ImageView mIv;
    private File file;

    Button btn_chat;
    ListView mLv;
    List<String> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIv = (ImageView) findViewById(R.id.iv_takePhone);
        mLv = (ListView) findViewById(R.id.lv_main);
        PermissionUtil.verifyStoragePermissions(this);
        mList = new ArrayList<>();
        mList.add("高德map");
        mList.add("地图选地址");
        mList.add("dialog");
        mList.add("raw");
        mList.add("BetterSpinnerActivity");
        mList.add("CornerLabelViewActivity");
        mList.add("PopwindowActivity");
        mList.add("AddressSelectorActivity");
        mLv.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return mList.size();
            }

            @Override
            public Object getItem(int position) {
                return mList.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView textView = new TextView(MainActivity.this);
                textView.setPadding(20, 20, 20, 20);
                textView.setTextSize(25);
                textView.setText(mList.get(position));
                return textView;
            }
        });
        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0://
                        startActivity(new Intent(MainActivity.this, MapActivity.class));
                        break;
                    case 1:
                        startActivity(new Intent(MainActivity.this, PlaceChooseActivity.class));
                        break;
                    case 2://dialog
                        startActivity(new Intent(MainActivity.this, DialogListActivity.class));
                        break;
                    case 3:
                        startActivity(new Intent(MainActivity.this, ReadRawsActivity.class));
                        break;
                    case 4:
                        startActivity(new Intent(MainActivity.this, BetterSpinnerActivity.class));
                        break;
                    case 5:
                        startActivity(new Intent(MainActivity.this, CornerLabelViewActivity.class));
                        break;
                    case 6:
                        startActivity(new Intent(MainActivity.this, PopwindowActivity.class));
                        break;
                    case 7:
                        startActivity(new Intent(MainActivity.this, AddressSelectorActivity.class));
                        break;

                    default:
                        break;
                }
            }
        });
        TakePhotoOptions.Builder builder = new TakePhotoOptions.Builder();
//        builder.setCorrectImage(true);
        builder.setWithOwnGallery(true);

        getTakePhoto().onEnableCompress(null, true);
        mIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                file = new File(Environment.getExternalStorageDirectory(), "/temp/" + System.currentTimeMillis() + ".jpg");
                if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
                Uri imageUri = Uri.fromFile(file);
                CropOptions.Builder builder1 = new CropOptions.Builder();
                builder1.setWithOwnCrop(true);
                getTakePhoto().onPickFromCaptureWithCrop(imageUri, builder1.create());
            }
        });
        btn_chat = (Button) findViewById(R.id.btn_chat);
        btn_chat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ChatActivity.class));
            }
        });
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
//        Glide.with(this).load(new File(result.getImages().get(0).getCompressPath())).into(mIv);
        Glide.with(this).load(file).into(mIv);
    }

    @Override
    public void takeFail(TResult result, String msg) {

    }

    @Override
    public void takeCancel() {

    }
}
